'use strict';

const Controller = require('egg').Controller;
/**
 * @Controller 权限模块
 */
class PermissionController extends Controller {
  /**
   * @Summary 登录获取菜单
   * @Description 登录获取菜单
   * @Router post /permission/login
   * @Request body loginParams
   * */
  async login() {
    const { username, password } = this.ctx.request.body;
    // 先判断用户是否存在
    // 判断账号和密码是否对应
    if (username === 'wxx' && password === '123') {
      this.ctx.body = {
        code: 20000,
        data: {
          token: this.service.permission.createToken({ username }),
          menu: [
            {
              path: '/home',
              name: 'Home',
              label: '首页',
              icon: 's-home',
              url: 'Home/index.vue',
            },
            {
              path: '/mall',
              name: 'Mall',
              label: '商品管理',
              icon: 'video-play',
              url: 'Mall/index.vue',
            },
            {
              path: '/user',
              name: 'user',
              label: '用户管理',
              icon: 'user',
              url: 'User/index.vue',
            },
            {
              label: '其他',
              name: 'other',
              icon: 'location',
              children: [
                {
                  path: '/page1',
                  name: 'page1',
                  label: '页面1',
                  icon: 'setting',
                  url: 'other/pageOne.vue',
                },
                {
                  path: '/page2',
                  name: 'page2',
                  label: '页面2',
                  icon: 'setting',
                  url: 'other/pageTwo.vue',
                },
              ],
            },
          ],
          message: '获取成功',
        },
      };
    } else if (username === 'admin' && password === '123') {
      this.ctx.body = {
        code: 20000,
        data: {
          token: this.service.permission.createToken({ username }),
          menu: [
            {
              path: '/home',
              name: 'Home',
              label: '首页',
              icon: 's-home',
              url: 'Home/index.vue',
            },
            {
              path: '/mall',
              name: 'mall',
              label: '商品管理',
              icon: 'video-play',
              url: 'Mall/index.vue',
            },
          ],
          message: '获取成功',
        },
      };
    } else {
      this.ctx.body = {
        code: -999,
        data: {
          message: '密码错误',
        },
      };
    }
  }
}

module.exports = PermissionController;
